package com.youlai.boot.system.controller;

import com.youlai.boot.system.service.ExamPaperService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.system.model.form.ExamPaperForm;
import com.youlai.boot.system.model.query.ExamPaperQuery;
import com.youlai.boot.system.model.vo.ExamPaperVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 试卷管理前端控制层
 *
 * @author youlaitech
 * @since 2025-07-15 09:39
 */
@Tag(name = "试卷管理接口")
@RestController
@RequestMapping("/api/v1/exam-paper")
@RequiredArgsConstructor
public class ExamPaperController  {

    private final ExamPaperService examPaperService;

    @Operation(summary = "试卷管理分页列表")
    @GetMapping("/page")
//    @PreAuthorize("@ss.hasPerm('system:exam-paper:query')")
    public PageResult<ExamPaperVO> getExamPaperPage(ExamPaperQuery queryParams ) {
        IPage<ExamPaperVO> result = examPaperService.getExamPaperPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增试卷管理")
    @PostMapping
//    @PreAuthorize("@ss.hasPerm('system:exam-paper:add')")
    public Result<Void> saveExamPaper(@RequestBody @Valid ExamPaperForm formData ) {
        boolean result = examPaperService.saveExamPaper(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取试卷管理表单数据")
    @GetMapping("/{id}/form")
//    @PreAuthorize("@ss.hasPerm('system:exam-paper:edit')")
    public Result<ExamPaperForm> getExamPaperForm(
        @Parameter(description = "试卷管理ID") @PathVariable Long id
    ) {
        ExamPaperForm formData = examPaperService.getExamPaperFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改试卷管理")
    @PutMapping(value = "/{id}")
//    @PreAuthorize("@ss.hasPerm('system:exam-paper:edit')")
    public Result<Void> updateExamPaper(
            @Parameter(description = "试卷管理ID") @PathVariable Long id,
            @RequestBody @Validated ExamPaperForm formData
    ) {
        boolean result = examPaperService.updateExamPaper(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除试卷管理")
    @DeleteMapping("/{ids}")
//    @PreAuthorize("@ss.hasPerm('system:exam-paper:delete')")
    public Result<Void> deleteExamPapers(
        @Parameter(description = "试卷管理ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = examPaperService.deleteExamPapers(ids);
        return Result.judge(result);
    }
}
